get description from enum c#

42

get description from enum c# -

GetEnumDescription<MyEnum>(1);
GetEnumDescription<YourEnum>(2);

get enum from enum description -

public static int GetEnumFromDescription(string description, Type enumType)
{
    foreach (var field in enumType.GetFields())
    {
        DescriptionAttribute attribute
            = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute))as DescriptionAttribute;
        if(attribute == null)
            continue;
        if(attribute.Description == description)
        {
            return (int) field.GetValue(null);
        }
    }
    return 0;
}

Comments

Submit
0 Comments